<?php
namespace api\controllers;

use api\models\User;
use deepziyu\yii\swoole\coroutine\Mysql;
use deepziyu\yii\swoole\pool\MysqlPool;
use deepziyu\yii\swoole\pool\RedisPool;
use deepziyu\yii\swoole\redis\Connection;
use Yii;
use yii\rest\Controller;
use deepziyu\yii\swoole\cache\SwooleCache;
use api\models\Name as SystemSetting;

/**
 * Site controller
 */
class SiteController extends Controller
{
    public function actionIndex($id = 1)
    {
        return [
            'version' => '1.0',
            'id' => $id,
        ];
    }

    public function actionList()
    {
        return User::find()->all();
    }

    public function actionSchema(){
        return Yii::$app->getDb()->getSchema()->getTableSchemas();
    }

    public function actionCacheGc()
    {
        Yii::$app->cache->flush();
        return [];
    }

    public function actionDb()
    {
        $res =  $this->testModel();
        return $res;
    }

    public function actionRedis()
    {
        /** @var Connection $redis */
        $redis = Yii::$app->redis;
        $redis->set('testKey','hello redis');
        $value = $redis->get('testKey');
        return $value;
    }

    public function actionRedisPool()
    {
        $pool = new RedisPool();
        $pool->executeCommand('set','hhhh','deepziyu');
        $value = $pool->executeCommand('get','hhhh');
        return [$pool,$value];
    }

    public function actionTransaction()
    {
        $transaction = User::getDb()->beginTransaction();
        User::getDb()->createCommand()->insert('de_session',[
            'id' => random_int(1,10000),
            'expire' => time(),
            'data' => json_encode(['fewf'=>111])
        ])->execute();
        //throw new \Exception(11);
        User::getDb()->createCommand()->insert('de_session',[
            'id' => random_int(10000,60000),
            'expire' => time(),
            'data' => json_encode(['fewf'=>222])
        ])->execute();
        //$transaction->commit();
        $transaction->rollBack();
        return [];
    }


}
